package com.jdd.modules.mqtt.server.revice;

import com.alibaba.fastjson.JSONObject;
import com.jdd.common.util.OssUtil;
import com.jdd.dao.entity.RuiKaTDeviceFaceDiscernLog;
import com.jdd.dao.service.RuiKaTDeviceFaceDiscernLogService;
import com.jdd.dao.service.TUserPassMediumService;
import com.jdd.dao.service.TVisitPassMediumService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @program: jdd-people-passage
 * @ClassName ReciveMqttQrResponseService
 * @description: 人脸识别结果topic消息处理类
 * @author: zsj
 * @create: 2022-03-24 09:56
 * @Version 1.0
 **/
@Service("ruikamqttFaceResponseStrategy")
public class RuiKaReciveMqttFaceResponseServiceStrategy implements IReciveMqttBaseStrategy {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RuiKaTDeviceFaceDiscernLogService ruiKaTDeviceFaceDiscernLogService;
    @Autowired
    private TVisitPassMediumService visitPassMediumService;
    @Autowired
    private TUserPassMediumService userPassMediumService;
    @Autowired
    private OssUtil ossUtil;

    /**
     * 处理从mqtt接收到的人脸识别结果消息接口
     *
     * @param message 消息
     * @return void
     * @Description
     * @Author zsj
     * @Date 2022-03-24 9:53
     **/
    @Override
    public void handleMessage(String message) {
        logger.info("接收到mqtt人脸识别结果消息，消息：{}", message);
        JSONObject jsonObject = JSONObject.parseObject(message);
        JSONObject body = jsonObject.getJSONObject("info");
        RuiKaTDeviceFaceDiscernLog faceDiscernLog = new RuiKaTDeviceFaceDiscernLog();
        faceDiscernLog.setIdCard(body.getString("idCard"));
        if (StringUtils.isNotBlank(body.getString("pic"))) {
            //上传阿里云
            String faceUrl = null;
            try {
                faceUrl = ossUtil.uploadBaseString2OSS(body.getString("pic").substring(23));
            } catch (IOException e) {
                logger.error("上传人脸图片失败：" + e.getMessage());
                e.printStackTrace();
            }
            faceDiscernLog.setPic(faceUrl);
        }
        //开门方式（机器配置）
        //0人脸开门方式
        //1远程开门方式
        //2远程开门或人脸开门
        //3人脸核验+远程开门
        faceDiscernLog.setOpendoorWay(body.getString("OpendoorWay"));
        //断点续传恢复
        faceDiscernLog.setRecordId(body.getString("RecordID"));
        //认证结果
        // 0无，
        // 1允许，
        // 2拒绝
        // 3还没有注册，
        // 22:待核验(开门方式为 3:人脸核验+
        //远程开门方式的控制记录)
        //24:无权限（特殊版本非通行时间段
        //的控制记录）
        faceDiscernLog.setVerifyStatus(body.getString("VerifyStatus"));
        faceDiscernLog.setIsNoMask(body.getString("isNoMask"));
        faceDiscernLog.setWhiteMatched(body.getString("similarity1"));
        faceDiscernLog.setBlackMatched(body.getString("similarity2"));
        faceDiscernLog.setPersionName(body.getString("persionName"));
        faceDiscernLog.setCustomId(body.getString("customId"));
        faceDiscernLog.setPersonType(body.getInteger("PersionType"));
        faceDiscernLog.setFacesluiceId(body.getString("facesluiceId"));
        faceDiscernLog.setFacesluiceName(body.getString("facesluiceName"));
        faceDiscernLog.setTemperature(body.getString("temperature"));
        faceDiscernLog.setUsec(body.getString("time"));
        faceDiscernLog.setDirection(body.getString("direction"));
        //保存识别结果
        ruiKaTDeviceFaceDiscernLogService.save(faceDiscernLog);
    }
}